import { ref } from 'vue'
export default function () {
  // 列表加载
  const listLoading = ref(false)
  // 获取列表数据
  const list = ref([])
  // 当前页数
  const page = ref(1)
  // 每页几行
  const rows = ref(10) // Assuming a default value, change as needed
  // 接口地址
  const localQueryUrl = ref('') // Specify the query URL
  //总页数
  const pageNum = ref(0)
  // 总记录数
  const count = ref(0)
  // 成功回调
  const getListSuccess = ref(null)
  // 查询条件默认为空
  const localQuery = ref({})
  // 加载状态
  const loadingStatus = ref('loading')

  const getList = (resetList: boolean, query: {}, queryUrl: string) => {
    console.log('query', query)
    localQuery.value = query
    localQueryUrl.value = queryUrl

    if (resetList) {
      list.value = []
      page.value = 1
    }

    const requestData = {
      page: page.value,
      rows: rows.value,
      ...query,
    }

    return uni.http({
      url: queryUrl,
      requestType: 'company',
      data: requestData,
    })
  }

  // 给list数据
  const setList = (response: any) => {
    if (response.code === 10000) {
      const e = response.data
      // 总条数
      pageNum.value = e.last_page
      // 总页数
      count.value = e.total
      e.data = e.data || []

      if (page.value > 1) {
        e.data.forEach((item: any) => {
          list.value.push(item)
        })
      } else {
        list.value = e.data
      }

      // if (getListSuccess.value) {
      //   getListSuccess.value(e)
      // }
    }

    listLoading.value = false
    return list.value
  }

  return {
    getList,
    setList,
    listLoading,
    list,
    loadingStatus,
  }
}
